package com.ruubypay.miss.digital.dubbo.service;

//import com.redick.annotation.LogMarker;
import com.ruubypay.miss.qrcode.api.IDigitalWalletService;
import com.ruubypay.miss.qrcode.hdwallet.req.*;
import com.ruubypay.miss.qrcode.hdwallet.res.*;
import com.ruubypay.miss.returntype.ModelsReturn;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.util.StringUtils;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author fj
 * @date 2024/8/18 21:27
 */
@DubboService(version = "1.0")
public class DigitalWalletServiceImpl implements IDigitalWalletService {
    private static final String SUCCESS_CODE = "00000000";
    private static final String SUCCESS_MESSAGE = "成功";

    @Override
    public ModelsReturn<MsgVerifyRes> msgVerify(MsgVerifyReq msgVerifyReq) {
        return null;
    }

    @Override
    public ModelsReturn<MsgSendRes> msgSend(MsgSendReq msgSendReq) {
        return null;
    }

//    @LogMarker(businessDescription = "钱包支付校验")
    @Override
    public ModelsReturn<WalletPayCheckRes> walletPayCheck(WalletPayCheckReq walletPayCheckReq) {
        try {
            TimeUnit.SECONDS.sleep(13);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        ModelsReturn<WalletPayCheckRes> modelsReturn = new ModelsReturn<>();
        modelsReturn.setResCode(SUCCESS_CODE);
        modelsReturn.setResMessage(SUCCESS_MESSAGE);
        WalletPayCheckRes resData = new WalletPayCheckRes();
//        checkFail10014361(resData);
//        checkFail13011(resData);
        checkSuccess(resData);
        modelsReturn.setResData(resData);
        return modelsReturn;
    }

    private static void checkFail10014361(WalletPayCheckRes resData) {
        resData.setContextId(null);
        resData.setPayChannelMsg("硬件钱包小额免密开关未打开");
        resData.setPayChannelStatus("10014361");
        resData.setTransMsg("交易失败");
        resData.setTransStatus("01");
    }

    private static void checkFail13011(WalletPayCheckRes resData) {
        resData.setContextId(null);
        resData.setPayChannelMsg("钱包余额超限");
        resData.setPayChannelStatus("13011");
        resData.setTransMsg("交易失败");
        resData.setTransStatus("01");
    }

    private static void checkSuccess(WalletPayCheckRes resData) {
        resData.setContextId(null);
        resData.setPayChannelMsg("unexpected error.");
        resData.setPayChannelStatus("0");
        resData.setTransMsg("交易成功");
        resData.setTransStatus("00");
    }

//    @LogMarker(businessDescription = "支付")
    @Override
    public ModelsReturn<WalletPayRes> walletPay(WalletPayReq walletPayReq) {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        ModelsReturn<WalletPayRes> modelsReturn = new ModelsReturn<>();
        modelsReturn.setResCode(SUCCESS_CODE);
        modelsReturn.setResMessage(SUCCESS_MESSAGE);
        WalletPayRes resData = new WalletPayRes();
        resData.setPayChannelMsg("unexpected error.");
        resData.setPayChannelStatus("0");
        resData.setTransStatus("00");
        resData.setTransMsg("交易成功");
        resData.setOrderId(walletPayReq.getOrderId());
        resData.setPaymentId("20240818230812" + String.format("%04d", new Random().nextInt(10000)));
        resData.setReceiptAmount(Integer.valueOf(walletPayReq.getBankDeductAmount()));
        resData.setBuyerPayAmount(resData.getReceiptAmount());
        resData.setDiscount(0);
        modelsReturn.setResData(resData);
        return modelsReturn;
    }

//    @LogMarker(businessDescription = "查询")
    @Override
    public ModelsReturn<WalletQueryRes> walletQuery(WalletQueryReq walletQueryReq) {
//        try {
//            TimeUnit.SECONDS.sleep(13);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }

//        throw new RuntimeException("walletQuery error");

        ModelsReturn<WalletQueryRes> modelsReturn = new ModelsReturn<>();
        modelsReturn.setResCode(SUCCESS_CODE);
        modelsReturn.setResMessage(SUCCESS_MESSAGE);
        WalletQueryRes resData = new WalletQueryRes();
        resData.setWalletForm("5");
        resData.setWalletStatus("1");
        resData.setCarrierType("2");
        resData.setContextId("0022422900571103");
        resData.setDeviceType("3");
        modelsReturn.setResData(resData);
        return modelsReturn;
    }

    @Override
    public ModelsReturn<WalletRepayRes> walletRepay(WalletRepayReq walletRepayReq) {
        return null;
    }
}
